program drop _all

/* THIS PROGRAM IMPLEMENTS LOGNORMAL MODEL-SURVIVAL REGRESSIONS IN TABLE 7 (cols. 3-4); IC-STATA ver. 10.1 
    NOTE: All the source and results files are saved at single directory: C:\RESTAT\
*/


program define DUR
clear all
set more off
set memory 80m
set matsize 800
use C:\RESTAT\data1.dta, replace
log using C:\RESTAT\Tab7_survival.log, replace
               
                    
                       tsset firm year

                    
  #delimit ;            
                    display" In case of 2 observations IndG missign, because no data there on sales in year 1994";
                    display"Replace these by IndG==0";
                    replace IndG=0 if primaryu==175 & year==1994;      

                                   
                    display "KEEP THE ALL POSSIBLE OBS. FOR EXIT EVEN THOSE WHICH MISSING FOR GROWTH";                   
                    keep if EI~=. & FG~=. & IndG~=. & ageT~=. & sales~=. & AS~=.& ES~=.& FORdirect~=. &
                          intang~=. & ESgap~=. & gap~=. & solvency~=. ; 
 
/* DUMMY FOR TECHNOLOGY leaders/leaders */

                    display"CREATE DUMMY TECH=1 FOR TECHNOLOGY leaders, TECH=0(leaders)";
                    gen TECH=0;
                    replace TECH=1 if tech_diffIND>=0 & tech_diffIND~=. & tech_diffIND~=99; 
                    replace TECH=. if  (tech_diffIND==.|tech_diffIND==99) ;
  
 
          /* gen firm-level means */
                   
                    sort firm ;
                    by firm: egen avsales=mean(sales);
                    by firm: egen avsales2=mean(sales2);
                    by firm: egen avESgap=mean(ESgap);
                    by firm: egen avFG=mean(FG);                
                    
                    by firm: egen avageT=mean(ageT);
                    by firm: egen avageT2=mean(ageT2);
                    by firm: egen avAS=mean(AS);
                    by firm: egen avES=mean(ES);
                    by firm: egen avFORdirect=mean(FORdirect);
                    by firm: egen avintang=mean(intang);
                    by firm: egen avgap=mean(gap);
                    by firm: egen avsolvency=mean(solvency);
                    by firm: egen MIN_ageT=min(ageT);

                    tsset firm year;
  
/* SET DATA FOR DURATION */
                    
                    /* USING AGE_T */
                    gen MAT=MIN_ageT-1 ;
                    stset ageT, id(firm) failure(EI==1) entry(MAT) ; 
                                               

/* TO AVOID PERFECT FAILURE/SUCCESS DETERMINATION PROBLEM RE-GROUP DUMMIES - APPLY RE-GROUPINGS FROM TAB4 FIRST  */

                    display" GROUP YEAR DUMMIES INTO 2 YEARS (94+95), (96+97), WHERE PERFECT FAIULRE DETERM. PROBLEM";
                    replace year94=1 if (year==1994|year==1995);
                    replace year97=1 if (year==1996|year==1997);
                    replace year00=1 if (year==2000|year==2001);
                    drop year95 year96 ; /* 2 years when perfect failure determination " */
                    /* so now yearly dummies are: 94,97, 98, 99,00 */                
         
                   replace primaryu=16 if US2==16;
                   replace primaryu=17 if US2==17;
                   replace primaryu=203 if (primaryu==204|primaryu==205);
                   replace primaryu=208 if primaryu==209;
                   replace primaryu=22 if (US2==22|US2==23);
                   replace primaryu=24 if  US2==24;
                   replace primaryu=25 if  US2==25;
                   replace primaryu=26 if (US2==26|US2==27|US2==28);
                   replace primaryu=317 if (primaryu==322|primaryu==325|primaryu==326|
                                           primaryu==327|primaryu==328);
                   replace primaryu=332 if primaryu==333;
                   replace primaryu=343 if primaryu==344;
                   replace primaryu=345 if primaryu==346;
                   replace primaryu=351 if primaryu==352;
                   replace primaryu=361 if primaryu==362;
                   replace primaryu=363 if primaryu==364;
                   replace primaryu=374 if primaryu==375;
                   replace primaryu=38 if (US2==38|US2==39);
                   replace primaryu=47 if (primaryu==472|primaryu==481|primaryu==491|primaryu==493);
                   replace primaryu=505 if (primaryu==506|primaryu==507|primaryu==508);
                   replace primaryu=515 if primaryu==516;
                   replace primaryu=519 if primaryu==523;
                   replace primaryu=554 if (primaryu==571|primaryu==581);
                   replace primaryu=591 if (primaryu==593|primaryu==594);
                   replace primaryu=621 if primaryu==628;
                   replace primaryu=721 if primaryu==729;
                   replace primaryu=735 if primaryu==737;
                   replace primaryu=792 if (primaryu==794|primaryu==799);
 
/* ADDITIONAL ADJUSTMENTS NEEDED FOR */             
                   display"TECHNOLOGY laggards"; 
                   keep if TECH==0 & TECH~=.;
                   replace primaryu=1 if (primaryu==16|primaryu==22);
                   replace primaryu=34 if (primaryu==317|primaryu==332|primaryu==341|primaryu==343|primaryu==345|primaryu==349);                   
                   replace primaryu=36 if (primaryu==361|primaryu==363);
                   replace primaryu=509 if primaryu==505;
                   replace primaryu=541 if primaryu==554;
                   replace primaryu=621 if primaryu==591;
                   replace primaryu=721 if primaryu==734;
                   replace year98=1 if (year97==1|year==1998);
                   drop year97; /* so now only year94,year98,year99,year00 */ 
                   
 

/* ************************* LOGNORMAL: Tab 7 (col. 3) TECHNOLOGY LAGGARDS **************************************** */
              
               xi:streg FG STATIC IndG sales sales2 ES FORdirect intang gap ESgap D89 solvency 
                          year94-year00 i.primaryu reg1-reg7 
                          avFG avsales avsales2 avES avFORdirect avintang 
                          avgap avESgap avsolvency,dist(lnormal) cluster(firm) tr;



           
/* *********************** REPEAT THE ANALYSIS NOW FOR TECHNOLOGY LEADERS ************* */
clear;
use C:\RESTAT\data1.dta, replace;

                    
                     tsset firm year;                    
            
                    display" In case of 2 observations IndG missign, because no data there on sales in year 1994";
                    display"Replace these by IndG==0";
                    replace IndG=0 if primaryu==175 & year==1994;      

                                   
                    display "KEEP THE ALL POSSIBLE OBS. FOR EXIT EVEN THOSE WHICH MISSING FOR GROWTH";                   
                    keep if EI~=. & FG~=. & IndG~=. & ageT~=. & sales~=. & AS~=.& ES~=.& FORdirect~=. &
                          intang~=. & ESgap~=. & gap~=. & solvency~=. ; 
 
/* DUMMY FOR TECHNOLOGY leaders/leaders */

                    display"CREATE DUMMY TECH=1 FOR TECHNOLOGY leaders, TECH=0(leaders)";
                    gen TECH=0;
                    replace TECH=1 if tech_diffIND>=0 & tech_diffIND~=. & tech_diffIND~=99; 
                    replace TECH=. if  (tech_diffIND==.|tech_diffIND==99) ;
   

          /* gen firm-level means */
                   
                    sort firm ;
                    by firm: egen avsales=mean(sales);
                    by firm: egen avsales2=mean(sales2);
                    by firm: egen avESgap=mean(ESgap);
                    by firm: egen avFG=mean(FG);                
                    
                    by firm: egen avageT=mean(ageT);
                    by firm: egen avageT2=mean(ageT2);
                    by firm: egen avAS=mean(AS);
                    by firm: egen avES=mean(ES);
                    by firm: egen avFORdirect=mean(FORdirect);
                    by firm: egen avintang=mean(intang);
                    by firm: egen avgap=mean(gap);
                    by firm: egen avsolvency=mean(solvency);
                    by firm: egen MIN_ageT=min(ageT);

                    tsset firm year;
                                                 
 /* SET DATA FOR DURATION */
                    
                    /* USING AGE_T */
                    gen MAT=MIN_ageT-1 ;
                    stset ageT, id(firm) failure(EI==1) entry(MAT) ; 
                    

/* TO AVOID PERFECT FAILURE/SUCCESS DETERMINATION PROBLEM RE-GROUP DUMMIES - APPLY RE-GROUPINGS FROM TAB4 FIRST */

                    display" GROUP YEAR DUMMIES INTO 2 YEARS (94+95), (96+97), WHERE PERFECT FAIULRE DETERM. PROBLEM";
                    replace year94=1 if (year==1994|year==1995);
                    replace year97=1 if (year==1996|year==1997);
                    replace year00=1 if (year==2000|year==2001);
                    drop year95 year96 ; /* 2 years when perfect failure determination " */
                    /* so now yearly dummies are: 94,97, 98, 99,00 */                
                 

                 
                   replace primaryu=16 if US2==16;
                   replace primaryu=17 if US2==17;
                   replace primaryu=203 if (primaryu==204|primaryu==205);
                   replace primaryu=208 if primaryu==209;
                   replace primaryu=22 if (US2==22|US2==23);
                   replace primaryu=24 if  US2==24;
                   replace primaryu=25 if  US2==25;
                   replace primaryu=26 if (US2==26|US2==27|US2==28);
                   replace primaryu=317 if (primaryu==322|primaryu==325|primaryu==326|
                                           primaryu==327|primaryu==328);
                   replace primaryu=332 if primaryu==333;
                   replace primaryu=343 if primaryu==344;
                   replace primaryu=345 if primaryu==346;
                   replace primaryu=351 if primaryu==352;
                   replace primaryu=361 if primaryu==362;
                   replace primaryu=363 if primaryu==364;
                   replace primaryu=374 if primaryu==375;
                   replace primaryu=38 if (US2==38|US2==39);
                   replace primaryu=47 if (primaryu==472|primaryu==481|primaryu==491|primaryu==493);
                   replace primaryu=505 if (primaryu==506|primaryu==507|primaryu==508);
                   replace primaryu=515 if primaryu==516;
                   replace primaryu=519 if primaryu==523;
                   replace primaryu=554 if (primaryu==571|primaryu==581);
                   replace primaryu=591 if (primaryu==593|primaryu==594);
                   replace primaryu=621 if primaryu==628;
                   replace primaryu=721 if primaryu==729;
                   replace primaryu=735 if primaryu==737;
                   replace primaryu=792 if (primaryu==794|primaryu==799);
 
 /* ADDITIONAL ADJUSTMENTS NEEDED FOR */            

                   display"TECHNOLOGY leaders";   
                   keep if TECH==1 & TECH~=.;
                   replace primaryu=202 if primaryu==203;
                   replace primaryu=343 if primaryu==345;
                   replace primaryu=354 if primaryu==351; 

  
/* ************************* LOGNORMAL: Tab 7 (col. 4) TECHNOLOGY LEADERS **************************************** */

               xi:streg FG STATIC sales sales2 ES FORdirect intang gap ESgap D89 solvency 
                          year94-year00 i.primaryu reg1-reg7 IndG
                          avFG avsales avsales2 avES avFORdirect avintang 
                          avgap avESgap avsolvency,dist(lnormal) cluster(firm) tr;

  


#delimit cr
log close
end
